package com.goldsprite.infinityworld.entities.gameworlds;

import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.goldsprite.gdxcore.entities.*;
import com.goldsprite.infinityworld.entities.components.*;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Align;
import com.goldsprite.infinityworld.entities.randommap.RandomMapEntity;

public class InfinityGameWorld extends GameWorld{
	public boolean gameInitialized;

	InfinityGameWorld world;
	private Touchpad rocker;
	private AnimatorEntity role;

	private Skin skin;

	private RockerRoleController roleCtrl;

	private FollowCameraContoller followCamCtrl;


	//初始化游戏世界
	public void initGameWorld(Skin skin, GameWorldGroup gameCanvasGroup) {
		this.skin = skin;
		////设置游戏世界视口宽高
		//实体世界画布
		world = this;
		world.setFillParent(true);
		gameCanvasGroup.addActor(world);
		world.setWorldGroup(gameCanvasGroup);
		world.init();

		//创建随机地图实体
		RandomMapEntity map = new RandomMapEntity();
		world.addEntity(map);
		map.init();

		//创建动画机实体
		{
			String roleTexPath = "roles/Soldier.png";
			Texture roleTex = new Texture(Gdx.files.internal(roleTexPath));

			//统一动画速度
			float animPerVel = 1;
			// 创建动画器并导入源材质与设置帧单位大小
			role = new AnimatorEntity(roleTex, 100, 100);
			role.bodyBound.set(15, 18);
			//设置动画
			role.addAnimation("Idle", 0, 6, animPerVel / 6);
			role.addAnimation("Walk", 1, 8, animPerVel / 8);
			role.setCurAnimation("Idle");
			//设置实体大小
			role.setScl(0.6f);
			role.setCenterPosition(role.getBodySize().x/2, role.getBodySize().y/2);
			world.addEntity(role);
		}


		//gui部分
		{
			//摇杆
			float size = 150, margin = 30;
			rocker = new Touchpad(0, skin);
			rocker.setBounds(margin, margin, size, size);
			gameCanvasGroup.addActor(rocker);
			//跟随相机开关
			float sizeX = 240, sizeY = 80, sizeScl = 0.4f;
			TextButton toggleFollowCamBtn = new TextButton("跟随相机(开)", skin);
			toggleFollowCamBtn.setSize(sizeX*sizeScl, sizeY*sizeScl);
			gameCanvasGroup.addActor(toggleFollowCamBtn);
			toggleFollowCamBtn.setPosition(margin, toggleFollowCamBtn.getParent().getHeight()-margin, Align.topLeft);
			toggleFollowCamBtn.addListener(new ClickListener(){
					boolean enanled = true;
					public void clicked(InputEvent ev, float x, float y){
						enanled = !enanled;
						followCamCtrl.enableFollowCam = enanled;
						toggleFollowCamBtn.setText(enanled ?"跟随相机(开)" :"跟随相机(关)");
					}
				});
			
			//跟随相机缩放开关
			TextButton toggleFollowCamSclBtn = new TextButton("跟随相机缩放(开)", skin);
			toggleFollowCamSclBtn.setSize(sizeX*sizeScl, sizeY*sizeScl);
			gameCanvasGroup.addActor(toggleFollowCamSclBtn);
			toggleFollowCamSclBtn.setPosition(margin*2+toggleFollowCamBtn.getWidth(), toggleFollowCamSclBtn.getParent().getHeight()-margin, Align.topLeft);
			toggleFollowCamSclBtn.addListener(new ClickListener(){
					boolean enanled = true;
					public void clicked(InputEvent ev, float x, float y){
						enanled = !enanled;
						followCamCtrl.enableFollowCamScl = enanled;
						toggleFollowCamSclBtn.setText(enanled ?"跟随相机缩放(开)" :"跟随相机缩放(关)");
					}
				});
		}
		
		//组件
		{
			roleCtrl = new RockerRoleController(role, rocker);
			followCamCtrl = new FollowCameraContoller(world.transCam, rocker,  role, 2.4f);
		}

		//标记初始化完成
		gameInitialized = true;
	}

	@Override
	public void act(float delta) {
		super.act(delta);

		if(gameInitialized){
			roleCtrl.update(delta);
			followCamCtrl.update(delta);
		}
	}
	
}
